setwd("~/Dropbox/heyTim/UKKeynesians/UKKeynesiansReplication/scripts")

# Setup the data from the raw Eurobarometer downloads.
# Not necessary with the public version of the replication archive
#source("eurobarometer_setup.r")

library(margins)
library(ggplot2)
library(zoo)
library(stargazer)
library(survey)

# Load the data that eurobarometer_setup.r created. We do it this way
# so that we don't have to distribute the full set of Eurobarometer data
# files, which we are not supposed to do.
load(file="../generated_data/eb_uk.rdata")

## model 1 = survey dates pooled by year
mod1 = lm(antidef ~ defsplitb + as.factor(syear)*econNatCurrent_rev_nodk, data=eb.uk)

## GB respondents only
mod1gb = lm(antidef ~ defsplitb + as.factor(syear)*econNatCurrent_rev_nodk, data=eb.uk[which(eb.uk$ctrycode == "GB-GBN"),])

stargazer(mod1, mod1gb, type = "text", style = "ajps")

cplot.1 = cplot(mod1, x="syear", dx="econNatCurrent_rev_nodk", what="effect", se.type="shade")
abline(h = 0, col = "grey", lty = 2)
cplot.1$syear = as.numeric(as.character(cplot.1$xvals))
ggplot(cplot.1, aes(x = syear)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_errorbar(aes(ymin = lower, ymax = upper, width=0.1)) +
  geom_line(aes(y = yvals)) +
  scale_x_continuous(breaks = seq(min(cplot.1$syear), max(cplot.1$syear),1)) +
  xlab("Year") +
  ylab("Countercyclicality") +
  theme_bw()
dev.print(file="../generated_images/uk_beta_ak.pdf",
          height=5,
          width=7,
          device=pdf)


cplot.1gb = cplot(mod1gb, x="syear", dx="econNatCurrent_rev_nodk", what="effect", se.type="shade")
abline(h = 0, col = "grey", lty = 2)
cplot.1gb$syear = as.numeric(as.character(cplot.1gb$xvals))
ggplot(cplot.1gb, aes(x = syear)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_errorbar(aes(ymin = lower, ymax = upper, width=0.1)) +
  geom_line(aes(y = yvals)) +
  scale_x_continuous(breaks = seq(min(cplot.1gb$syear), max(cplot.1gb$syear),1)) +
  xlab("Year") +
  ylab("Countercyclicality") +
  theme_bw()
dev.print(file="../generated_images/gb_beta_ak.pdf",
          height=5,
          width=7,
          device=pdf)


mod1sq = lm(antidef ~ defsplitb + factor(occup) + factor(gender) + as.factor(sdate)*econNatCurrent_rev_nodk, weights = eb.uk$weightUK, data=eb.uk)
cplot.1sq = cplot(mod1sq, x="sdate", dx="econNatCurrent_rev_nodk", what="effect", se.type="shade")
abline(h = 0, col = "grey", lty = 2)

cplot.1sqkeep = cplot.1sq

cplot.1sq$sdate = as.Date(as.character(levels(cplot.1sq$xvals))[cplot.1sq$xvals], format = "%Y-%m-%d")
cplot.1sq$waven = as.numeric(cplot.1sq$xvals)
cplot.1sq$wavename = c("May \n 2010", "Nov \n 2010", "May \n 2011", "Nov \n 2011", "May \n 2012", "Nov \n 2012", "May \n 2013", "Nov \n 2013", "May \n 2014", "Nov \n 2014", "May \n 2015", "Nov \n 2015", "May \n 2016", "Nov \n 2016", "May \n 2017", "Nov \n 2017")
ggplot(cplot.1sq, aes(x = waven)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_linerange(aes(ymin = lower, ymax = upper), colour="grey") +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.1sq$waven, labels = cplot.1sq$wavename) +
  xlab("Date") +
  ylab("Countercyclicality") +
  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
  panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5))
dev.print(file="../generated_images/uk_beta_ak_date_wtd.pdf",
          height=5,
          width=7,
          device=pdf)
ggplot(cplot.1sq, aes(x = waven)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_linerange(aes(ymin = lower, ymax = upper), colour="grey") +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.1sq$waven, labels = cplot.1sq$wavename) +
  xlab("Date") +
  ylab("Countercyclicality") +
  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
    panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5)) +
  geom_vline(xintercept=c(12.7,13.2), colour="red", linetype="dashed")
dev.print(file="../generated_images/uk_beta_ak_date_wtd_expdates.pdf",
          height=5,
          width=7,
          device=pdf)


mod1.gbsq = lm(antidef ~ defsplitb + factor(occup) + factor(gender) + as.factor(sdate)*econNatCurrent_rev_nodk, weights = eb.uk$weightUK[which(eb.uk$ctrycode == "GB-GBN")], data=eb.uk[which(eb.uk$ctrycode == "GB-GBN"),])
cplot.1.gbsq = cplot(mod1.gbsq, x="sdate", dx="econNatCurrent_rev_nodk", what="effect", se.type="shade")
abline(h = 0, col = "grey", lty = 2)

cplot.1.gbsqkeep = cplot.1.gbsq

cplot.1.gbsq$sdate = as.Date(as.character(levels(cplot.1.gbsq$xvals))[cplot.1.gbsq$xvals], format = "%Y-%m-%d")
cplot.1.gbsq$waven = as.numeric(cplot.1.gbsq$xvals)
cplot.1.gbsq$wavename = c("May \n 2010", "Nov \n 2010", "May \n 2011", "Nov \n 2011", "May \n 2012", "Nov \n 2012", "May \n 2013", "Nov \n 2013", "May \n 2014", "Nov \n 2014", "May \n 2015", "Nov \n 2015", "May \n 2016", "Nov \n 2016", "May \n 2017", "Nov \n 2017")
ggplot(cplot.1.gbsq, aes(x = waven)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_linerange(aes(ymin = lower, ymax = upper), colour="grey") +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.1.gbsq$waven, labels = cplot.1.gbsq$wavename) +
  xlab("Date") +
  ylab("Countercyclicality") +
  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
    panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5))
dev.print(file="../generated_images/gb_beta_ak_date_wtd.pdf",
          height=5,
          width=7,
          device=pdf)

mod1sq.lr3.0 = lm(antidef ~ defsplitb + as.factor(sdate)*econNatCurrent_rev_nodk, weights = eb.uk$weightUK[which(eb.uk$lr3 == 0)], data=subset(eb.uk, lr3 == 0))
cplot.1sq.lr3.0 = cplot(mod1sq.lr3.0, x="sdate", dx="econNatCurrent_rev_nodk", what="effect", se.type="shade")
abline(h = 0, col = "grey", lty = 2)
cplot.1sq.lr3.0$sdate = as.Date(as.character(levels(cplot.1sq.lr3.0$xvals))[cplot.1sq.lr3.0$xvals], format = "%Y-%m-%d")
cplot.1sq.lr3.0$waven = as.numeric(cplot.1sq.lr3.0$xvals)
cplot.1sq.lr3.0$wavename = c("May \n 2010", "Nov \n 2010", "May \n 2011", "May \n 2014", "Nov \n 2014", "May \n 2015", "Nov \n 2015", "May \n 2016", "Nov \n 2016", "May \n 2017", "Nov \n 2017")
ggplot(cplot.1sq.lr3.0, aes(x = waven)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_linerange(aes(ymin = lower, ymax = upper), colour="grey") +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.1sq.lr3.0$waven, labels = cplot.1sq.lr3.0$wavename) +
  xlab("Date") +
  ylab("Countercyclicality") +
  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
    panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5))
dev.print(file="../generated_images/uk_beta_ak_date_wtd_lr3_0.pdf",
          height=5,
          width=7,
          device=pdf)

mod1sq.lr3.1 = lm(antidef ~ defsplitb + as.factor(sdate)*econNatCurrent_rev_nodk, weights = eb.uk$weightUK[which(eb.uk$lr3 == 1)], data=subset(eb.uk, lr3 == 1))
cplot.1sq.lr3.1 = cplot(mod1sq.lr3.1, x="sdate", dx="econNatCurrent_rev_nodk", what="effect", se.type="shade")
abline(h = 0, col = "grey", lty = 2)
cplot.1sq.lr3.1$sdate = as.Date(as.character(levels(cplot.1sq.lr3.1$xvals))[cplot.1sq.lr3.1$xvals], format = "%Y-%m-%d")
cplot.1sq.lr3.1$waven = as.numeric(cplot.1sq.lr3.1$xvals)
cplot.1sq.lr3.1$wavename = c("May \n 2010", "Nov \n 2010", "May \n 2011", "May \n 2014", "Nov \n 2014", "May \n 2015", "Nov \n 2015", "May \n 2016", "Nov \n 2016", "May \n 2017", "Nov \n 2017")
ggplot(cplot.1sq.lr3.1, aes(x = waven)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_linerange(aes(ymin = lower, ymax = upper), colour="grey") +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.1sq.lr3.1$waven, labels = cplot.1sq.lr3.1$wavename) +
  xlab("Date") +
  ylab("Countercyclicality") +
  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
    panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5))
dev.print(file="../generated_images/uk_beta_ak_date_wtd_lr3_1.pdf",
          height=5,
          width=7,
          device=pdf)

mod1sq.lr3.2 = lm(antidef ~ defsplitb + as.factor(sdate)*econNatCurrent_rev_nodk, weights = eb.uk$weightUK[which(eb.uk$lr3 == 2)], data=subset(eb.uk, lr3 == 2))
cplot.1sq.lr3.2 = cplot(mod1sq.lr3.2, x="sdate", dx="econNatCurrent_rev_nodk", what="effect", se.type="shade")
abline(h = 0, col = "grey", lty = 2)
cplot.1sq.lr3.2$sdate = as.Date(as.character(levels(cplot.1sq.lr3.2$xvals))[cplot.1sq.lr3.2$xvals], format = "%Y-%m-%d")
cplot.1sq.lr3.2$waven = as.numeric(cplot.1sq.lr3.2$xvals)
cplot.1sq.lr3.2$wavename = c("May \n 2010", "Nov \n 2010", "May \n 2011", "May \n 2014", "Nov \n 2014", "May \n 2015", "Nov \n 2015", "May \n 2016", "Nov \n 2016", "May \n 2017", "Nov \n 2017")
ggplot(cplot.1sq.lr3.2, aes(x = waven)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_linerange(aes(ymin = lower, ymax = upper), colour="grey") +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.1sq.lr3.2$waven, labels = cplot.1sq.lr3.2$wavename) +
  xlab("Date") +
  ylab("Countercyclicality") +
  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
    panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5))
dev.print(file="../generated_images/uk_beta_ak_date_wtd_lr3_2.pdf",
          height=5,
          width=7,
          device=pdf)

xtab.pre2013 = round(100*prop.table(table(eb.uk$antidef[which(eb.uk$quarter < 2013)], eb.uk$lr3[which(eb.uk$quarter < 2013)]), 2), 1)
xtab.post2014 = round(100*prop.table(table(eb.uk$antidef[which(eb.uk$quarter > 2014)], eb.uk$lr3[which(eb.uk$quarter > 2014)]), 2), 1)

xtab.pre2013.mat = matrix(xtab.pre2013, nrow=4)
colnames(xtab.pre2013.mat) <- c("Left", "Centre", "Right")
rownames(xtab.pre2013.mat) <- c("1", "2", "3", "4")

xtab.post2014.mat = matrix(xtab.post2014, nrow=4)
colnames(xtab.post2014.mat) <- c("Left", "Centre", "Right")
rownames(xtab.post2014.mat) <- c("1", "2", "3", "4")

stargazer(xtab.pre2013.mat
          #, type="text"
          , digits=1
          , title="Cross-tabulation of (by-column) percentages of $AntiDeficit_i$ against a three-way categorization of left--right self-placement, for the May~2010--May~2011 surveys. ``Left'' $\\Rightarrow LeftRight_i \\in \\{1,2,3,4\\}$; ``Centre'' $\\Rightarrow LeftRight_i = 5$; ``Right'' $\\Rightarrow LeftRight_i \\in \\{6,7,8,9,10\\}$"
          , label="tab:EB:xtab.pre2013"
          , table.placement="htp"
          , out="../generated_tex/xtab_pre2013.tex"
          )
stargazer(xtab.post2014.mat
          #, type="text"
          , digits=1
          , title="Cross-tabulation of (by-column) percentages of $AntiDeficit_i$ against a three-way categorization of left--right self-placement, for surveys from May~2015 (inclusive). ``Left'' $\\Rightarrow LeftRight_i \\in \\{1,2,3,4\\}$; ``Centre'' $\\Rightarrow LeftRight_i = 5$; ``Right'' $\\Rightarrow LeftRight_i \\in \\{6,7,8,9,10\\}$"
          , label="tab:EB:xtab.post2014"
          , table.placement="htp"
          , out="../generated_tex/xtab_post2014.tex"
          )

## weight UK adjusts British and NI 
# note that "weights = weightUK" in the lm() function returns the same results as the estimate from the survey weighted svyglm

eb.uk.w = svydesign(ids = ~1, data = eb.uk, weights = eb.uk$weightUK)
mod1w = svyglm(antidef ~ defsplitb + as.factor(sdate)*econNatCurrent_rev_nodk, family = gaussian(), data=eb.uk, design = eb.uk.w)
mod1wr = lm(antidef ~ defsplitb + as.factor(sdate)*econNatCurrent_rev_nodk, weights = eb.uk$weightUK, data=eb.uk)

#### using only the subset of respondents who see the question identical to the one we field in our experiment, i.e. defsplitb = 0
mod1wrs = lm(antidef ~ as.factor(sdate)*econNatCurrent_rev_nodk, weights = eb.uk$weightUK[which(eb.uk$defsplitb == 0)], data=eb.uk[which(eb.uk$defsplitb == 0),])

cplot.1wrs = cplot(mod1wrs, x="sdate", dx="econNatCurrent_rev_nodk", what="effect", se.type="shade")
abline(h = 0, col = "grey", lty = 2)
cplot.1wrs$sdate = as.Date(as.character(levels(cplot.1wrs$xvals))[cplot.1wrs$xvals], format = "%Y-%m-%d")
cplot.1wrs$waven = as.numeric(cplot.1wrs$xvals)
cplot.1wrs$wavename = c("May \n 2010", "Nov \n 2010", "May \n 2011", "Nov \n 2011", "May \n 2012", "Nov \n 2012", "May \n 2013", "Nov \n 2013", "May \n 2014", "Nov \n 2014", "May \n 2015", "Nov \n 2015", "May \n 2016", "Nov \n 2016", "May \n 2017", "Nov \n 2017")
ggplot(cplot.1wrs, aes(x = waven)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_errorbar(aes(ymin = lower, ymax = upper, width=0.1)) +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.1wrs$waven, labels = cplot.1wrs$wavename) +
  xlab("Date") +
  ylab("Countercyclicality") +
  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
  panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5))
   # theme_bw() 
dev.print(file="../generated_images/uk_beta_ak_date_wtd_splitA.pdf",
          height=5,
          width=7,
          device=pdf)

#### split B
mod1wrsb = lm(antidef ~ as.factor(sdate)*econNatCurrent_rev_nodk, weights = eb.uk$weightUK[which(eb.uk$defsplitb == 1)], data=eb.uk[which(eb.uk$defsplitb == 1),])
cplot.1wrsb = cplot(mod1wrsb, x="sdate", dx="econNatCurrent_rev_nodk", what="effect", se.type="shade")
abline(h = 0, col = "grey", lty = 2)
cplot.1wrsb$sdate = as.Date(as.character(levels(cplot.1wrsb$xvals))[cplot.1wrsb$xvals], format = "%Y-%m-%d")
cplot.1wrsb$waven = as.numeric(cplot.1wrsb$xvals)
cplot.1wrsb$wavename = c("May \n 2010", "Nov \n 2010", "May \n 2011", "Nov \n 2011", "May \n 2012", "Nov \n 2012", "May \n 2013", "Nov \n 2013", "May \n 2014", "Nov \n 2014", "May \n 2015", "Nov \n 2015", "May \n 2016", "Nov \n 2016", "May \n 2017", "Nov \n 2017")
ggplot(cplot.1wrsb, aes(x = waven)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_errorbar(aes(ymin = lower, ymax = upper, width=0.1)) +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.1wrsb$waven, labels = cplot.1wrsb$wavename) +
  xlab("Date") +
  ylab("Countercyclicality") +
  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
  panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5))
   # theme_bw() 
dev.print(file="../generated_images/uk_beta_ak_date_wtd_splitB.pdf",
          height=5,
          width=7,
          device=pdf)

#### including controls:
mod1wrx = lm(antidef ~ defsplitb + as.factor(year)*econNatCurrent_rev_nodk + as.factor(gender) + age + as.factor(occup), weights = eb.uk$weightUK, data=eb.uk)

cplot.1wr = cplot(mod1wr, x="year", dx="econNatCurrent_rev_nodk", what="effect", se.type="shade")
abline(h = 0, col = "grey", lty = 2)
## makes little difference to subtantive conclusions

#### deficit preferences trends over time
mod2 = lm(antidef ~ defsplitb + as.factor(sdate), data=eb.uk, weights = eb.uk$weightUK)
cplot.2 = cplot(mod2, x="sdate", what="prediction")
  
cplot.2$sdate = as.Date(cplot.2$xvals, format = "%Y-%m-%d")
cplot.2$waven = as.numeric(as.factor(cplot.2$xvals))
cplot.2$wavename = c("May \n 2010", "Nov \n 2010", "May \n 2011", "Nov \n 2011", "May \n 2012", "Nov \n 2012", "May \n 2013", "Nov \n 2013", "May \n 2014", "Nov \n 2014", "May \n 2015", "Nov \n 2015", "May \n 2016", "Nov \n 2016", "May \n 2017", "Nov \n 2017")
ggplot(cplot.2, aes(x = waven)) +
  geom_hline(yintercept=2.5, color="gray70") + 
  geom_errorbar(aes(ymin = lower, ymax = upper, width=0.1)) +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.2$waven, labels = cplot.2$wavename) +
  xlab("Date") +
  ylab("Opposition to debts/deficits") +
  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
  panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5))

dev.print(file="../generated_images/uk_antidef_wtd.pdf",
          height=5,
          width=7,
          device=pdf)

mod3 = lm(econNatCurrent_rev_nodk ~ as.factor(sdate), weights = eb.uk$weightUK, data=eb.uk)
cplot.3 = cplot(mod3, x="sdate", what="prediction")
cplot.3$sdate = as.Date(cplot.3$xvals, format = "%Y-%m-%d")
cplot.3$waven = as.numeric(as.factor(cplot.3$xvals))
cplot.3$wavename = c("May \n 2010", "Nov \n 2010", "May \n 2011", "Nov \n 2011", "May \n 2012", "Nov \n 2012", "May \n 2013", "Nov \n 2013", "May \n 2014", "Nov \n 2014", "May \n 2015", "Nov \n 2015", "May \n 2016", "Nov \n 2016", "May \n 2017", "Nov \n 2017")
ggplot(cplot.3, aes(x = waven)) +
  geom_hline(yintercept=2.5, color="gray70") + 
  geom_errorbar(aes(ymin = lower, ymax = upper, width=0.1)) +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.3$waven, labels = cplot.3$wavename) +
  xlab("Date") +
  ylab("Perceptions of the national economy") +
  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
  panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5))
  
  
dev.print(file="../generated_images/uk_econNatCurrent_wtd.pdf",
          height=5,
          width=7,
          device=pdf)

#### some simple controls




mod1b = lm(antidef ~ defsplitb + as.factor(sdate)*econNatCurrent_rev_nodk + as.factor(lr3), weights = eb.uk$weightUK, data=eb.uk)

cplot.1b = cplot(mod1b, x="sdate", dx="econNatCurrent_rev_nodk", what="effect", se.type="shade")
abline(h = 0, col = "grey", lty = 2)
cplot.1b$sdate = as.Date(as.character(levels(cplot.1b$xvals))[cplot.1b$xvals], format = "%Y-%m-%d")
cplot.1b$waven = as.numeric(cplot.1b$xvals)
cplot.1b$wavename = c("May \n 2010", "Nov \n 2010", "May \n 2011", "May \n 2014", "Nov \n 2014", "May \n 2015", "Nov \n 2015", "May \n 2016", "Nov \n 2016", "May \n 2017", "Nov \n 2017")
ggplot(cplot.1b, aes(x = waven)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_errorbar(aes(ymin = lower, ymax = upper, width=0.1)) +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.1b$waven, labels = cplot.1b$wavename) +
  xlab("Date") +
  ylab("Countercyclicality") +
  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
  panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5))
   # theme_bw() 
dev.print(file="../generated_images/uk_beta_ak_date_conLR3_wtd.pdf",
          height=5,
          width=7,
          device=pdf)


## For response to reviewers: overlay average perceptions of the economy trends with countercyclicality AK plot
dim(eb.uk)
names(eb.uk)

ecNatCurrAgg = by(eb.uk$econNatCurrent_rev_nodk, eb.uk$syear, mean, na.rm = T)
antidefAgg = by(eb.uk$antidef, eb.uk$syear, mean, na.rm = T)
antidefAggSurv =c(by(eb.uk$antidef, eb.uk$sdate, mean, na.rm = T))

### 
ggplot(cplot.1, aes(x = syear)) +
  geom_hline(yintercept=0, color="gray70") + 
  geom_errorbar(aes(ymin = lower, ymax = upper, width=0.1)) +
  geom_line(aes(y = yvals)) +
  scale_x_continuous(breaks = seq(min(cplot.1$syear), max(cplot.1$syear),1)) +
  geom_line(aes(y = ecNatCurrAgg)) +
  
  xlab("Year") +
  ylab("Countercyclicality") +
  theme_bw()

### rescale axes for overlay
ggplot(cplot.1, aes(x = syear)) +
  #geom_hline(yintercept=0, color="gray70") + 
  geom_errorbar(aes(ymin = lower, ymax = upper, width=0.1), color="transparent") +
  geom_line(aes(y = yvals)) +
  scale_x_continuous(breaks = seq(min(cplot.1$syear), max(cplot.1$syear),1)) +
  geom_line(aes(y = ecNatCurrAgg/5-0.45), linetype="dashed" ) +
  scale_y_continuous(sec.axis = sec_axis(~(.+0.45)*5, name = "Average Economic Perception"))+
## add deficit aggregate also
#  geom_line(aes(y = antidefAgg/5-.55), linetype="dotted" ) +
  xlab("Year") +
  ylab("Countercyclicality") +
  theme_bw()

dev.print(file="../generated_images/uk_beta_ak_ecNatCurrAgg.pdf",
          height=5,
          width=7,
          device=pdf)

## with objective economic growth data
## https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/ihyp/pn2
yoyGrowth = c(1.7, 1.6, 1.4, 2.0, 2.9, 2.3, 1.8, 1.8)

ggplot(cplot.1, aes(x = syear)) +
  #geom_hline(yintercept=0, color="gray70") + 
  geom_errorbar(aes(ymin = lower, ymax = upper, width=0.1), color="transparent") +
  geom_line(aes(y = yvals)) +
  scale_x_continuous(breaks = seq(min(cplot.1$syear), max(cplot.1$syear),1)) +
  geom_line(aes(y = yoyGrowth/10-0.15), linetype="dashed" ) +
  scale_y_continuous(sec.axis = sec_axis(~(.+0.15)*10, name = "Year-on-Year GDP Growth (%)"))+
  ## add deficit aggregate also
  #  geom_line(aes(y = antidefAgg/5-.55), linetype="dotted" ) +
  xlab("Year") +
  ylab("Countercyclicality") +
  theme_bw()

dev.print(file="../generated_images/uk_beta_ak_yoyGrowth.pdf",
          height=5,
          width=7,
          device=pdf)

## antideficit and performance perceptions
ggplot(cplot.1, aes(x = syear)) +
  #geom_hline(yintercept=0, color="gray70") + 
#  geom_errorbar(aes(ymin = lower, ymax = upper, width=0.1), color="transparent") +
  geom_line(aes(y = antidefAgg)) +
  scale_x_continuous(breaks = seq(min(cplot.1$syear), max(cplot.1$syear),1)) +
  geom_line(aes(y = ecNatCurrAgg), linetype="dashed" ) +
  #scale_y_continuous(sec.axis = sec_axis(~(.+0.15)*10, name = "Year-on-Year GDP Growth (%)"))+
  ## add deficit aggregate also
  #  geom_line(aes(y = antidefAgg/5-.55), linetype="dotted" ) +
  xlab("Year") +
  ylab("Economic perceptions, antideficit preferences") +
  theme_bw()

dev.print(file="../generated_images/uk_ecNatCurrAgg_antidefAgg.pdf",
          height=5,
          width=7,
          device=pdf)

#### count sample sizes for descriptives in supplementary material: 
nsrv = rep(NA, length(unique(eb.uk$srv)))

for(i in 1:length(unique(eb.uk$srv))){
  nsrv[i] = length(which(eb.uk$srv == unique(eb.uk$srv)[i]))  
}
nsrv

### betaAK and levels of borrowing preferences

## ggplot (cplot.1sq), ad antidefAgg as geom_line
ggplot(cplot.1sq, aes(x = waven)) +
  geom_hline(yintercept=0, color="gray70") + 
 # geom_linerange(aes(ymin = lower, ymax = upper), colour="grey") +
  geom_line(aes(y = yvals)) +
  scale_x_continuous( breaks = cplot.1sq$waven, labels = cplot.1sq$wavename) +
  geom_line(aes(y = antidefAggSurv/5-.55), linetype="dashed") +
  xlab("Date") +
  ylab("Countercyclicality") +
    scale_y_continuous(sec.axis = sec_axis(~(.+0.55)*5, name = "Average Antideficit Preference"))+

  theme(#axis.text.x = element_text(angle = 70, margin = margin(t = 20)), 
  panel.background =  element_rect(fill = "white", colour = NA), 
    panel.border =   element_rect(fill = NA, colour="grey50"), 
    panel.grid.major = element_line(colour = "grey90", size = 0.2),
    panel.grid.minor = element_line(colour = "grey98", size = 0.5))



dev.print(file="../generated_images/uk_beta_ak_date_wtd_antiDAgg.pdf",
          height=5,
          width=7,
          device=pdf)
